
class ExcavationTerrain{
  constructor(viewer) {
    this.viewer = viewer
  }

  remove(){
    //移除分析的开挖区域
    this.viewer.scene.globe.removeAllExcavationRegion();
  }

  excavation(){
    const digPolygon = new Cesium.DrawHandler(this.viewer, Cesium.DrawMode.Polygon, 0);
    digPolygon.activeEvt.addEventListener((isActive)=>{
      //开挖激活后改变cursor
      if(isActive == true){
        this.viewer.enableCursorStyle = false;
        this.viewer._element.style.cursor = '';
        document.body.classList.remove('drawCur')
        document.body.classList.add('drawCur')
      }
      else{
        this.viewer.enableCursorStyle = true;
        document.body.classList.remove('drawCur')
      }
    });
    digPolygon.drawEvt.addEventListener((result)=>{
      //计算地形开挖的顶点坐标，经纬度表达
      const array = [].concat(result.object.positions);
      const positions = [];
      let i = 0, len = array.length;
      for(; i < len; i ++){
        const cartographic = Cesium.Cartographic.fromCartesian(array[i]);
        const longitude = Cesium.Math.toDegrees(cartographic.longitude);
        const latitude = Cesium.Math.toDegrees(cartographic.latitude);
        const h = cartographic.height;
        if(positions.indexOf(longitude)==-1&&positions.indexOf(latitude)==-1){
          positions.push(longitude);
          positions.push(latitude);
          positions.push(h);
        }
      }
      //开挖深度
      const dep = 20;
      this.viewer.scene.globe.removeAllExcavationRegion();
      //开挖参数配置
      this.viewer.scene.globe.addExcavationRegion({
        name : 'excavation' ,
        position : positions,
        height : dep,
        transparent : false
      });
      //交互绘制线面不显示
      digPolygon.polygon.show = false;
      digPolygon.polyline.show = false;
      digPolygon.deactivate();
    });
    digPolygon.activate();
  }
}
export { ExcavationTerrain }
